package sanrenxing.dayEnd.backRepertoryTotal.utils;

import org.springframework.stereotype.Component;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Days;
import sanrenxing.dayEnd.backRepertoryTotal.service.DaysBiz;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期推算
 */
@Component("uts")
public class uts {
    //从数据库获得日期
    @Resource
    private DaysBiz daysIm;
    //xx是输入日期，ts是t+？，根据输入的日期和t+？,得出非节假日期
    public String sds(String xx,int ts){
        //创建日期对象
        SimpleDateFormat ds=new SimpleDateFormat("yyyy-MM-dd");
        List<Days> lt=daysIm.all();
        String kkm="";
        long ll= 0;
        try {
           ll = ds.parse(xx).getTime();
           int kk=ts;
           for (int j=1;j<=kk;j++){
               ll=ll+1000*60*60*24;
               boolean bl=true;
                   Date dr=new Date(ll);
               System.out.println(dr.getDay());
               if(dr.getDay()!=0&&dr.getDay()!=6){
                       for (int i=0;i<lt.size();i++){
                           Date dt=ds.parse(lt.get(i).getHolidaydate());
                           if(dr.getTime()>=dt.getTime()&&dr.getTime()<=dt.getTime()+1000*60*60*24*lt.get(i).getDays()){
                               bl=false;
                               ll=ds.parse(lt.get(i).getEnddate()).getTime();
                               System.out.println(j);
                               break;
                           }
                       }
                       if(bl){
                           ll=dr.getTime();
                       }
                   }else{
                       j-=1;
                       System.out.println(j+"ds"+new Date(ll).getDay()+"sd"+new Date(ll).toString());
                   }
           }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ds.format(new Date(ll));
    }


    public boolean ifHoliday(Date date){
        try {
            List<Days> lt=daysIm.all();
            SimpleDateFormat ds=new SimpleDateFormat("yyyy-MM-dd");
            if(date.getDay()!=0&&date.getDay()!=6){
                for (int i=0;i<lt.size();i++){
                    Date dt=ds.parse(lt.get(i).getHolidaydate());
                    if(date.getTime()>=dt.getTime()&&date.getTime()<=dt.getTime()+1000*60*60*24*lt.get(i).getDays()){
                        return true;
                    }
                }

            }else{
                return true;
            }
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
